package cn.yy.cm.config;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 描述: Mapper工厂Bean->生成代理类
 *
 * @author: yanglin
 * @Date: 2021-11-29-10:27
 * @Version: 1.0
 */
public class DFactoryBean implements FactoryBean {

    /**
     * 接口T
     */
    private Class mapperInterface;

    /**
     * Mybatis SqlSession
     */
    private SqlSession sqlSession;

    public DFactoryBean(Class mapperInterface) {
        this.mapperInterface = mapperInterface;
    }

    /**
     * 设置SqlSession
     * @param sqlSessionFactory
     */
    @Autowired
    public void setSqlSession(SqlSessionFactory sqlSessionFactory) {
        // 将Mapper接口配置加入到Configuration
        // sqlSessionFactory.getConfiguration().addMapper(mapperInterface);
        this.sqlSession = sqlSessionFactory.openSession();
    }

    @Override
    public Object getObject() throws Exception {
        return sqlSession.getMapper(mapperInterface);
    }

    @Override
    public Class<?> getObjectType() {
        return mapperInterface;
    }
}
